Ext.ns('ru.proburo.oms.catalog.components');

/**
 * ru.proburo.oms.catalog.components.Tree
 * @extends ru.proburo.oms.components.AbstractTree
 */
ru.proburo.oms.catalog.components.Tree = Ext.extend(ru.proburo.oms.components.AbstractTree, {
	initLoader: function()
	{
		if(true === Ext.isAir)
		{
			return new ru.proburo.oms.air.dao.TreeLoader({
				dbFile: ru.proburo.oms.Constants.DATABASE,				
				query:  '\
					SELECT \
						c1.id, \
						c1.name, \
						"silk-folder" AS iconCls, \
						CASE WHEN cnt.item_count > 0 THEN c1.name  ||  " ("  ||  cnt.item_count  ||  ")" ELSE c1.name END AS text,\
						CASE WHEN c2.childs THEN NULL ELSE "true" END AS leaf \
					FROM \
						Catalog AS c1 \
					LEFT JOIN \
					( \
						SELECT \
							parent_id, \
							COUNT(*) AS childs \
						FROM \
							Catalog \
						GROUP BY \
							parent_id \
					) AS c2 \
					ON \
						c1.id = c2.parent_id \
					LEFT JOIN \
					( \
						SELECT \
							ci.node_id, \
							COUNT(*) AS item_count \
						FROM \
							CatalogItem AS ci \
						LEFT JOIN \
							Item AS i \
						ON \
							ci.item_id = i.id \
						WHERE \
							i.blocked = 0 \
							AND ci.visible = 1 \
						GROUP BY \
							ci.node_id \
					) AS cnt \
					ON \
						c1.id = cnt.node_id \
					WHERE \
						c1.parent_id = ? \
						AND c1.visible = 1 \
					ORDER BY c1.sort_index \
				'
			});
		}
		else
		{
			return new Ext.tree.TreeLoader({
				dataUrl: '/services/catalog/node/list.json'
			});
		}
	},
	initRoot: function()
	{
		return new Ext.tree.AsyncTreeNode({
			id: 	  1,
			text: 	  'root',
			expanded: true
		})
	}	
});